# coding=utf-8
from __future__ import unicode_literals
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import User
from django.db import models

class WorkFlow(models.Model):
    name = models.CharField(verbose_name=u'工作流名称', max_length=100)
    content_type = models.ForeignKey(ContentType,verbose_name=u'关联表')
    is_open = models.BooleanField(verbose_name=u'是否启用',default=True)

    class Meta:
        verbose_name = u'工作流'
        verbose_name_plural = u'工作流管理'

    def __unicode__(self):
        return '%s' % (self.name)

class Node(models.Model):
    workflow = models.ForeignKey(WorkFlow,verbose_name='工作流')
    name = models.CharField(verbose_name=u'节点名称', max_length=100)
    step = models.IntegerField(u'级别',default=1)
    process = models.ManyToManyField('NodeProcess',verbose_name='流程',blank=True,related_name='node_process')

    class Meta:
        verbose_name = u'节点'
        verbose_name_plural = u'节点管理'

    def __unicode__(self):
        return '%s-%s' % (self.workflow.name,self.name)

class NodeProcess(models.Model):
    KIND_CHOICES = ((1, u'审批通过'), (2, u'审批不通过'))
    nextnode = models.ForeignKey(Node,verbose_name='下一步节点')
    kind = models.IntegerField(u'审批类型',choices=KIND_CHOICES,default=1)

    class Meta:
        verbose_name = u'节点流程'
        verbose_name_plural = u'节点流程管理'

    def __unicode__(self):
        return '%s-%s' % (self.nextnode.name,self.kind)

class History(models.Model):
    STATUS_CHOICES = ((1, u'进行中'), (2, u'已完成'), (3, u'待修改'), (4, u'终止'))
    workflow = models.ForeignKey(WorkFlow,verbose_name='工作流')
    detail_id = models.IntegerField(u'关联详情id')
    user = models.ForeignKey(User,verbose_name='申请人',related_name='start_person')
    node = models.ForeignKey(Node,verbose_name='当前节点')
    create_at = models.DateTimeField(u'创建时间', auto_now_add=True)
    finish_at = models.DateTimeField(u'完成时间',blank=True,null=True)
    status = models.IntegerField(u'当前状态',choices=STATUS_CHOICES,default=1)
    content = models.TextField(u'备注',blank=True,null=True)

    class Meta:
        verbose_name = u'工作流历史'
        verbose_name_plural = u'工作流历史管理'

    def __unicode__(self):
        return '%s-%s' % (self.workflow,self.detail_id)

class HistoryDetail(models.Model):
    STATUS_CHOICES = ((1, u'提交'), (2, u'审批通过'), (3, u'审批不通过'))
    history = models.ForeignKey(History,verbose_name='工作流历史')
    node = models.ForeignKey(Node,verbose_name='节点')
    approve_person = models.ForeignKey(User,verbose_name='审批人',related_name='approve_person',blank=True,null=True)
    create_at = models.DateTimeField(u'创建时间', auto_now_add=True)
    status = models.IntegerField(u'状态',choices=STATUS_CHOICES,default=1)
    content = models.TextField(u'备注',blank=True,null=True)

    class Meta:
        verbose_name = u'历史详情'
        verbose_name_plural = u'历史详情管理'

    def __unicode__(self):
        return '%s' % (self.history)